package com.prs.crm.report;

import java.util.UUID;

import org.apache.struts2.ServletActionContext;
import org.courser.Util;


public class ReportManagerImpl implements ReportManager {
	
	public final static String TMP_PATH = "/report/tmp";
	
	
	private ReportExport pdfExport;
	

	private ReportExport xlsExport;
	
	public ReportExport getPdfExport() {
		return pdfExport;
	}

	public void setPdfExport(ReportExport pdfExport) {
		this.pdfExport = pdfExport;
	}
	
	public ReportExport getXlsExport() {
		return xlsExport;
	}

	public void setXlsExport(ReportExport xlsExport) {
		this.xlsExport = xlsExport;
	}

	public String export(ReportTable table,ExportConfig config,int type)
	{
		String path =Util.getFullPath(Util.combinURI( Util.createPathByDate(
				TMP_PATH, ServletActionContext.getServletContext()),getFileName(type)),
				ServletActionContext.getServletContext());
		
		
			xlsExport.export(table, path, config);
		
		return path;
	}
	/**
	 * 销售订单
	 */
	public String exportSaleOrder(ReportTable table,ExportConfig config,int type)
	{
		String path =Util.getFullPath(Util.combinURI( Util.createPathByDate(
				TMP_PATH, ServletActionContext.getServletContext()),getFileName(type)),
				ServletActionContext.getServletContext());
		
		if (type == REPORTTYPE_PDF)
		{
			pdfExport.export(table, path, config);
		}
		else
		{
			xlsExport.exportSaleOrder(table, path, config);
		}
		return path;
	}
	
	private String getFileName(int type)
	{
		if (type == REPORTTYPE_PDF)
		{
			return UUID.randomUUID().toString().replace("-", "")+".pdf";
		}
		else if (type == REPORTTYPE_EXCEL)
		{
			return UUID.randomUUID().toString().replace("-", "")+".xls";
		}
		return null;
	}

	public String exportAlreadyDistribution(ReportTable table,
			ExportConfig config, int type) {
		
		String path =Util.getFullPath(Util.combinURI( Util.createPathByDate(
				TMP_PATH, ServletActionContext.getServletContext()),getFileName(type)),
				ServletActionContext.getServletContext());
		
		if (type == REPORTTYPE_PDF)
		{
			pdfExport.export(table, path, config);
		}
		else
		{
			xlsExport.exportAlreadyDistribution(table, path, config);
		}
		return path;
	}

	
}
